package com.bite.book.controller;

import com.bite.book.constant.Constants;
import com.bite.book.enums.BookStatusEnum;
import com.bite.book.enums.ResultCodeEnum;
import com.bite.book.mode.*;
import com.bite.book.service.BookService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
@Slf4j
@RequestMapping("/book")
@RestController
public class BookController {
 //进行登录成功后返回图书链表的接口的设定；
 @Autowired
 private BookService bookService;
//@RequestMapping("/getList")
//public List<BookInfo> getList(){
// return bookService.getList();
//}

 @RequestMapping(value = "/addBook",produces = "application/json")
 public String addBook(BookInfo bookInfo) {
 log.info("进行图书的添加，request:{}",bookInfo);
  if (!StringUtils.hasLength(bookInfo.getBookName())
          || !StringUtils.hasLength(bookInfo.getAuthor())
          || bookInfo.getPrice() == null
          || bookInfo.getCount() == null
          || !StringUtils.hasLength(bookInfo.getPublish())
          || bookInfo.getStatus() == null) {
   log.error("添加图书不合法，request:{}",bookInfo);
   return "参数不合法";
  }
  try {
   bookService.addBook(bookInfo);
   return "";
  } catch (Exception e) {
   log.error("添加图书异常，e:{}",e);
   return "添加图书异常";
  }
  }
 //进行根据页码进行返回对应页的图书链表；

 @RequestMapping("/getListByPage")
 public Result<ResponseResult<BookInfo>> getListByPage(PageRequest pageRequest, HttpSession session){
//  if(session.getAttribute(Constants.SESSION_USER_KEY)==null){
//   return Result.unlogin();
//  }
//
//  //进行强制登录操作的设置；
//  UserInfo userInfo=(UserInfo)session.getAttribute(Constants.SESSION_USER_KEY);
////1，进行参数的校验；
////由于页码和总页面个数都有默认值，不需要进行检验；
//  //2.进行数据的返回，这时就进行mapper的相关考虑；
//  if(userInfo==null||userInfo.getId()<=0){
//   //用户没有登录；
////   Result result=new Result();
////   result.setCode(ResultCodeEnum.UNLOGIN);
////   result.setErrMag("用户没有登录");
////   result.setData(null);
////   return result;
//   return Result.unlogin();
//  }
  ResponseResult<BookInfo> listByPage=bookService.getListByPage(pageRequest);
  Result result=new Result();
  return Result.success(listByPage);
//  result.setCode(ResultCodeEnum.SUCCESS);
//  result.setErrMag("");
//  result.setData(listByPage);
//  return result;
   //return listByPage;
//  return Result.success(listByPage);
 }
 //3.进行查询图书操作；
 @RequestMapping("/queryBookById")
 public BookInfo queryBookById(Integer bookId){
  log.info("查询图书，bookId"+bookId);
  return bookService.queryBookById(bookId);
 }
 //4.进行修改图书操作；
 @RequestMapping(value = "/updateBook")
 public Result updateBook(BookInfo bookInfo) {
  log.info("修改图书，bookInfo: {}", bookInfo);
  try {
   //修改成功；
   bookService.updateBook(bookInfo);
   return Result.success("");
  } catch (Exception e) {
   log.error("修改图书发生异常，e:", e);
   return Result.fail("修改图书发生异常");
  }
 }
 //进行图书的删除；
 @RequestMapping(value = "/deleteBook" )
 public Result deleteBook(Integer bookId){
  log.info("删除图书，bookId:{}",bookId);
  try{
   BookInfo bookInfo=new BookInfo();
   bookInfo.setId(bookId);
   bookInfo.setStatus(BookStatusEnum.DELETED.getCode());
   bookService.updateBook(bookInfo);
   return Result.success("");
  }catch(Exception e){
   log.error("删除图书发生异常，e"+e);
   return Result.fail("删除图书发生异常");
  }
 }
 //进行批量删除书籍；
 @RequestMapping("/batchDelete")
 public Boolean batchDelete(@RequestParam List<Integer> ids){
  log.info("批量删除图书：ids {}",ids);
  try{
   bookService.batchDelete(ids);
   return true;
  }catch(Exception e){
   log.error("批量删除图书失败 e:",e);
   return false;

  }
 }
}

